package cn.edu.ujn.lab3.web;

import cn.edu.ujn.lab3.dao.*;
import cn.edu.ujn.lab3.dao.Record;
import cn.edu.ujn.lab3.service.*;
import cn.edu.ujn.lab3.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 进出记录管理控制器类
 */
@Controller
@RequestMapping("/record")
public class RecordController {
	// 依赖注入
	@Autowired
	private BaseDictService baseDictService;
    @Autowired
    private VehicleService vehicleService;
    @Autowired
    private RecordService recordService;
    @Autowired
    CustomerService customerService;
    @Autowired
    ParkAreaService parkAreaService;


	/**
	 *  记录列表
	 */
	@RequestMapping(value = "/list.action")
	public String list(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer rows, Integer cId, Integer vId, String vehicleNo, Integer pId, String state
			, Model model) {
		// 条件查询所有记录
		Page<Record> records = recordService
				.findRecordList(page, rows, cId,
                        vId,vehicleNo,pId,state);
        List<Customer> customers = customerService.selectList(new Customer());
        List<Vehicle> vehicles = vehicleService.selectList(new Vehicle());
        List<ParkArea> parkAreas = parkAreaService.selectList(new ParkArea());
        model.addAttribute("page", records);
        model.addAttribute("customers", customers);
        model.addAttribute("vehicles", vehicles);
        model.addAttribute("parkAreas", parkAreas);
		model.addAttribute("cId", cId);
		model.addAttribute("vId", vId);
		model.addAttribute("vehicleNo", vehicleNo);
        model.addAttribute("pId", pId);
        model.addAttribute("state", state);
		return "record";
	}
	
	/**
	 * 创建记录
	 */
	@RequestMapping("/create.action")
	@ResponseBody
	public String recordCreate(Record record,HttpSession session) {

        Vehicle ve = vehicleService.getVehicleById(record.getvId());
        record.setVehicleNo(ve.getVehicleNo());
        Customer customerById = customerService.getCustomerById(record.getcId());
        record.setcName(customerById.getCust_name());
        // 创建Date对象
        Date date = new Date();
        // 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Timestamp timeStamp = new Timestamp(date.getTime());
        record.setCreatedTime(timeStamp);
        int rows = recordService.createRecord(record);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	} 

	/**
	 * 通过id获取记录信息
	 */
	@RequestMapping("/getRecordById.action")
	@ResponseBody
	public Record getRecordById(Integer id) {
        Record record = recordService.getRecordById(id);
	    return record;
	}
	/**
	 * 更新记录
	 */
	@RequestMapping("/update.action")
	@ResponseBody
	public String recordUpdate(Record record) {
        Vehicle ve = vehicleService.getVehicleById(record.getvId());
        record.setVehicleNo(ve.getVehicleNo());
        Customer customerById = customerService.getCustomerById(record.getcId());
        record.setcName(customerById.getCust_name());
	    int rows = recordService.updateRecord(record);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}

	/**
	 * 删除记录
	 */
	@RequestMapping("/delete.action")
	@ResponseBody
	public String recordDelete(Integer id) {
	    int rows = recordService.deleteRecord(id);
	    if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}

}

